package com.caine.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caine.common.entity.R;
import com.caine.goods.entity.Goods;
import com.caine.goods.service.IGoodsService;
import com.caine.goods.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author V
 * @since 2021-01-04
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;

    @RequestMapping("getStock")
    public BigDecimal getStock(@RequestBody OrderVO vo) {
        BigDecimal stock = new BigDecimal(0);
        Goods goods = goodsService.getById(vo.getGoodsId());
        if (goods != null) {
            stock = goods.getStock();
        }
        return stock;
    }

    @RequestMapping("getGoods")
    public Goods getGoodsDetail(@RequestBody OrderVO vo) {
        return goodsService.getById(vo.getGoodsId());
    }

    @RequestMapping("getGoodsList")
    public R getGoodsList(@RequestBody OrderVO vo) {
        return R.success(goodsService.list(new LambdaQueryWrapper<Goods>().like(vo.getGoodsName() != null, Goods::getName, vo.getGoodsName())));
    }

    @RequestMapping("reduceStock")
    public boolean reduceStock(@RequestBody OrderVO vo) {
       return goodsService.reduceStock(vo);
    }
}
